package service;

import dao.BookingMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Booking;
import utlis.MybatisUtils;

import java.util.List;

public class BookingService {
    public static void main(String[] args) {
         BookingService sb  = new BookingService();
        System.out.println(sb.getBooking());
    }
    //查询
    public List<Booking> getBooking(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookingMapper bg = sqlSession.getMapper(BookingMapper.class);
        List<Booking> bookings = bg.getBooking();
        sqlSession.close();
        return bookings;
    }
    //添加
    public int addBooking(Booking booking){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookingMapper bookingMapper = sqlSession.getMapper(BookingMapper.class);
        int result = bookingMapper.addBooking(booking);

        sqlSession.commit();
        return result;
    }
    //id查询
    public Booking queryId(int id){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookingMapper bookingMapper = sqlSession.getMapper(BookingMapper.class);
        Booking booking = bookingMapper.queryId(id);

        sqlSession.close();
        return booking;
    }
    //删除
    public int deleteBooking(int id){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookingMapper bookingMapper = sqlSession.getMapper(BookingMapper.class);
        int result = bookingMapper.deleteBooking(id);
        sqlSession.commit();
        sqlSession.close();
        return result;
    }
    //修改
    public int updateBooking(Booking booking){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookingMapper bookingMapper = sqlSession.getMapper(BookingMapper.class);
        int result = bookingMapper.updateBooking(booking);
        sqlSession.commit();
        sqlSession.close();
        return result;
    }
    //分页
    public static List<Booking>page(int pageNo, int pageSize){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookingMapper bookingMapper = sqlSession.getMapper(BookingMapper.class);
        List<Booking> bookings = bookingMapper.page(pageNo,pageSize);
        sqlSession.close();
        return bookings;
    }
    //记录数
    public int count(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        BookingMapper bookingMapper = sqlSession.getMapper(BookingMapper.class);
        int result = bookingMapper.count();
        sqlSession.close();
        return result;
    }
}
